package org.zyx.goods.controller;


import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.zyx.entity.SearchParams;
import org.zyx.entity.SearchResult;
import org.zyx.service.SearchService;

import javax.annotation.Resource;

/**
 * @Author: SwordMaple
 * @Date: 2020/9/24  20:44
 * @Description:
 * @Version: 1.0
 */
@RestController
@RequestMapping
public class SearchHandler {

    @Resource
    private SearchService searchService;


    /**
     * 根据参数分页查询商品,post请求为了传递更多参数
     * @param searchParams 前端传递参数为json对象,使用 @RequestBody 接收
     * @return
     */
    @PostMapping("/page")
    public ResponseEntity<SearchResult> search(@RequestBody SearchParams searchParams){

        SearchResult result = searchService.search(searchParams);
        if(result == null || CollectionUtils.isEmpty(result.getItems())){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(result);
    }






}
